﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PocketPond.Core;

namespace PocketPond
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();

            txtInterval.Text = ClientSettings.RefreshInterval.ToString();
            txtUsername.Text = ClientSettings.Username;
            txtPassword.Text = ClientSettings.Password;
            chkUnreadOnly.Checked = ClientSettings.OnlyUnread;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                int.Parse(txtInterval.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Invalid Time interval!", "Error");
                return;
            }
            FileStream fs = new FileStream( Core.Utils.getPocketPondDir() + "\\settings", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(txtInterval.Text);
            sw.WriteLine(txtUsername.Text);
            sw.WriteLine(txtPassword.Text);
            if (chkUnreadOnly.Checked)
            {
                sw.WriteLine("true");
            }
            else
            {
                sw.WriteLine("false");
            }
            sw.Close();
            fs.Close();

            // update ClientSettings
            ClientSettings.RefreshInterval = int.Parse(txtInterval.Text.Trim());
            ClientSettings.Username = txtUsername.Text;
            ClientSettings.Password = txtPassword.Text;
            ClientSettings.OnlyUnread = chkUnreadOnly.Checked;

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}